﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AntiObesity.Base;
using AntiObesity.Base.Managers;

namespace AntiObesity.Services
{
    public partial class EditUserInfo : SecuredPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var user = Session.GetCurrentUser();
                var history = BodyHistoryManager.GetLatestHistory(user.ID);

                txtDateOfBirth.Text = user.DateOfBirth.ToShortDateString();

                txtHeight.Text = history.Height.ToString("#.##");
                txtWeight.Text = history.Weight.ToString("#.##");
                rduSex.SelectedIndex = user.Sex == false /* Male */ ? 0 : 1;

                DataBind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            // TODO: server side validation
            var user = Session.GetCurrentUser();
            user.Sex = Convert.ToBoolean(rduSex.SelectedValue);
            user.DateOfBirth = Convert.ToDateTime(txtDateOfBirth.Text);

            UserManager.Persist(user);

            BodyHistoryManager.PersistHistory(
                user.ID,
                (float)Convert.ToDouble(txtWeight.Text),
                (float)Convert.ToDouble(txtHeight.Text));

            Response.Redirect("UserHome.aspx");
        }
    }
}
